<?php
/**
 * Created by PhpStorm.
 * User: tangmusen
 * Date: 2017/9/1
 * Time: 10:04
 */

namespace app\admin\controller;
use app\admin\model\Cash;
use app\admin\model\LoginLog;
use app\admin\model\Members;
use app\admin\model\Recharge;
use app\api\controller\HX;
use think\Db;
use think\Request;

class Member extends Admin
{

    /**
     * 会员信息首页
     */
    public function index(Request $request){
        $mobile   = $request->param('mobile');
        $from     = $request->param('from');
        $to       = $request->param('to');
        $uid      = $request->param('uid');
        $t_uid    = $request->param('t_uid');
        $w=[];
        if($from && $to){
            $from = strtotime($from);
            $to = strtotime($to);
            $w['create_at'] = [['>=',$from],['<=',$to]];
        }
        if($mobile){
            $w['mobile']=$mobile;
        }
        if($uid){
            $w['id']    =$uid;
        }
        if($t_uid){
            $w['tid']   =$t_uid;
        }
        $list = Members::where($w)->order('id desc')->paginate(20);
        return view('index',[
            'list'=>$list,
            'page'=>$list->render(),
        ]);
    }

    /**
     * 查看会员信息
     */
    public function show_list(Request $request){
            $id = $request->param('id');
            $info = Members::get($id);
        
            return view('edit',[
                'info'=>$info
            ]);
    }

    /**
     * 修改会员信息
     */
    public function edit(Request $request){

        if($request->isPost()){
            $data = $request->post();
        
            $Member = new Members();
            $map=array(
                'id'=>$data['id']
            );
            $result=$Member->editData($map,$data);
            if ($result) {

                $this->success_new('修改成功');
            }else{
                $this->error_new('修改失败');
            }
        }
    }

    /**
     * 登陆日志
     */
    public function login_log(Request $request){
        $mobile        = $request->param('mobile');
        $from          = $request->param('from');
        $to            = $request->param('to');
        $login_way     = $request->param('login_way');
        $w = [];
        if($from && $to){
            $from = strtotime($from);
            $to   = strtotime($to);
            $w['create_at'] = [['>=',$from],['<=',$to]];
        }
        if($mobile){
            $uid = Members::where(array('mobile'=>$mobile))->value('id');
            $w['uid'] = $uid;
        }
        if($login_way){
            $w['login_way'] = $login_way;
        }
        $list = LoginLog::where($w)->order('id desc')->paginate(20,false,['query' => request()->param()]);
        $data = $list->all();
        foreach ( $data as $k=>$v){
            $v['minute'] = ceil((time()-strtotime($v['create_at']))/3600);
            $list[$k] = $v;
        }
        return view('login_log',[
            'list'=>$list,
            'page'=>$list->render(),
        ]);
    }
    
    /**
     * 冻结用户
     */
    public function edit_frozen(Request $request){
        $id = $request->post('id');
        $data['m_status'] =1;
        $result = Db::name('member')->where(array('id'=>$id))->update($data);
        add_member_msg($id,'您的账号已被冻结，有任何疑意，请联系客服!');
        if($result){
            json_return(200,'成功');
        }else{
            json_return(500,'失败');
        }

    }
    
    /**
     * 解冻用户
     */
    public function edit_unfreeze(Request $request){
        $id = $request->post('id');
        $data['m_status'] =0;
        $result = Db::name('member')->where(array('id'=>$id))->update($data);
        add_member_msg($id,'恭喜您,您的账号已解冻!');
        if($result){
            json_return(200,'成功');
        }else{
            json_return(500,'失败');
        }

    }

    /**
     * 删除用户
     */
    public function del(Request $request){
        $id          = $request->param('id');
        $result      = Db::name('member')->where(array('id'=>$id))->delete($id);
        if ($result) {
            $this->success_new('删除成功');
        }else{
            $this->error_new('删除失败');
        }
    }

    /**
     * 机器人信息
     */
    public function robot(Request $request){
        $cate    = $request->param('cate');
        $hall    = $request->param('hall');
        $w=[];
        if($cate){
            $w['r.cate'] = $cate;
        }
        if($hall){
            $w['r.hall'] = $hall;
        }
        $list = Db::name('robot')
            ->alias('r')
            ->field('r.*,c.name as cate_name,h.name as hall_name')
            ->join('dl_cate c','r.cate=c.id')
            ->join('dl_hall h','h.cate=r.cate and h.hall=r.hall')
            ->where($w)
            ->order('r.id desc')
            ->paginate(20);
        $cates = Db::name('cate')->select();
        return view('robot',[
            'cates'=>$cates,
            'list'=>$list,
            'page'=>$list->render(),
        ]);
    }

    /**
     * 改变机器人开启关闭状态
     */
    public function edit_open(Request $request){
        $id     = $request->post('id');
        $status = $request->post('status');
        $result = Db::name('robot')
            ->where(array('id'=>$id))
            ->update(array('status'=>$status));
        if($result){
            json_return(200,'成功');
        }else{
            json_return(500,'失败');
        }
    }

    /**
     * 添加机器人信息
     */
    public function add_robot(Request $request){
        $data = $request->post();
        $result=Db::name('robot')->insert($data);
        if ($result) {
            $this->success_new('添加成功',url('Admin/member/robot'));
        }else{
            $this->error_new('添加失败');
        }
    }

    /**
     * 查看机器人信息
     */
    public function robot_show(Request $request){
        $id = $request->param('id');
        $info = Db::name('robot')->where(array('id'=>$id))->find();
        $cates = Db::name('cate')->select();
        return view('robot_edit',[
            'cates'=>$cates,
            'data'=>$info
        ]);
    }

    /**
     * 修改机器人信息
     */
    public function robot_edit(Request $request){
        if($request->isPost()){
            $data = $request->post();
            $map=array(
                'id'=>$data['id']
            );
            $result=Db::name('robot')->where($map)->update($data);
            if ($result) {
                $this->success_new('修改成功',url('Admin/member/robot'));
            }else{
                $this->error_new('修改失败');
            }
        }
    }

    /**
     * 添加用户
     */
    public function add_member(Request $request){
        $mobile     = $request->post('mobile');
        $password   = $request->post('password');
        $User       = Db::name('member');
        $user_info = $User->where(array('mobile'=>$mobile))->find();
        if($user_info){
            $this->error_new('账号已经注册');exit();
        }
        $data['nickname']   = 'yicai'.rand(10000,99999);
        $data['mobile']     = $mobile;
        $data['password']   = md5($password);
        $data['create_at']  = time();
        $data['ip'] = get_client_ip();
        $data['token'] = md5(rand(0000,9999));
        if ($User->insert($data)) {
            $this->success_new('添加用户成功',url('Admin/member/index'));
        } else {
            $this->error_new('添加用户失败');
        }
    }

}